package com.sinochem.yunlian.ship.base.rpc;

import com.alibaba.fastjson.JSONObject;
import com.sinochem.yunlian.ship.base.biz.BsDataDictItemBiz;
import com.sinochem.yunlian.ship.base.entity.BsDataDictItem;
import com.sinochem.yunlian.ship.base.vo.response.DictItemVo;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@IgnoreToken
@RequestMapping("api/dataDictItem")
public class BsDataDictItemRest {
	
	@Autowired
	private BsDataDictItemBiz bsDataDictItemBiz;
	@Autowired
    private RedisTemplate redisTemplate;
	
	@RequestMapping(value = "getItemName/{id}", method = RequestMethod.GET)
	public String getItemName(@PathVariable("id") Long id) {
		
		return bsDataDictItemBiz.getItemName(id);
	}
	
	@RequestMapping(value = "getItemNameByItemValue/{dictCode}/{itemValue}")
	public String getItemNameByItemValue(@PathVariable("dictCode")String dictCode, @PathVariable("itemValue")long itemValue) {
		
		HashMap<Object, Object> hashMap = new HashMap<>();
		
		hashMap.put("dictCode", dictCode);
		
		hashMap.put("itemValue", itemValue);
		
		return bsDataDictItemBiz.getItemNameByItemValueByMap(hashMap);
	}
	
	/**
	 * 加载数字字典
	 * @param dictCode
	 * @return
	 */
	@RequestMapping("listDictItem/{dictCode}")
	public List<DictItemVo> listDictItem(@PathVariable(name = "dictCode") String dictCode) {
		List<DictItemVo> resultList =  new ArrayList<>();
		Map<String,DictItemVo> itemMap = null;
		String key = String.format(RedisKey.KEY_DICT_ITEM_MAP_PREFIX,dictCode);
		if (redisTemplate.hasKey(key)) {
			itemMap = redisTemplate.opsForHash().entries(key);
			for(Map.Entry<String,DictItemVo> entry :itemMap.entrySet()){
				resultList.add(entry.getValue());
			}
			return resultList;
		} else {
			List<BsDataDictItem> list = bsDataDictItemBiz.findByDictCodeForView(dictCode);
			Map<String,DictItemVo> dictMap = new HashMap<>(list.size());
			for (BsDataDictItem item : list) {
				DictItemVo vo = new DictItemVo();
				vo.setId(item.getId());
				vo.setDictCode(item.getDictCode());
				vo.setDictId(item.getDictId());
				vo.setItemCode(item.getItemCode());
				vo.setItemName(item.getItemName());
				vo.setItemValue(item.getItemValue());
				vo.setShowOrder(item.getShowOrder());
				dictMap.put(vo.getItemValue()+"",vo);
				resultList.add(vo);
			}
			redisTemplate.opsForHash().putAll(key,dictMap);
		}
		return resultList;
	}

	/**
	 * 加载数字字典(有序)
	 * @param dictCode
	 * @return
	 */
	@RequestMapping(value = "listDictItemOrder/{dictCode}",method = RequestMethod.GET)
	public List<DictItemVo> listDictItemOrder(@PathVariable(name = "dictCode") String dictCode) {
		List<DictItemVo> resultList =  new ArrayList<>();
		//Map<String,DictItemVo> itemMap = null;
		String key = String.format(RedisKey.KEY_DICT_ITEM_LIST_ORDER_PREFIX,dictCode);
		if (redisTemplate.hasKey(key)) {
			resultList  = redisTemplate.opsForList().range(key,0,-1);
			return resultList;
		} else {
			List<BsDataDictItem> list = bsDataDictItemBiz.findByDictCodeForView(dictCode);
			//Map<String,DictItemVo> dictMap = new HashMap<>(list.size());
			for (BsDataDictItem item : list) {
				DictItemVo vo = new DictItemVo();
				vo.setId(item.getId());
				vo.setDictCode(item.getDictCode());
				vo.setDictId(item.getDictId());
				vo.setItemCode(item.getItemCode());
				vo.setItemName(item.getItemName());
				vo.setItemValue(item.getItemValue());
				vo.setShowOrder(item.getShowOrder());
				vo.setItemGroup(item.getItemGroup());
				//	dictMap.put(vo.getItemValue()+"",vo);
				resultList.add(vo);
			}
			redisTemplate.opsForList().rightPushAll(key,resultList);
		}
		return resultList;
	}
}
